<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	$fromuser=$_RQ['fromuser'];
	$coupon=pdo_get('jy_weishop_coupon_sendticket_share',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$coupon['limittime']=json_decode($coupon['limittime'],true);
	if($coupon['limittime']['tstart']>TIMESTAMP){
		$coupon['timestatus']=1;
	}
	if($coupon['limittime']['tstart']<TIMESTAMP && $coupon['limittime']['tend']>TIMESTAMP){
		$coupon['timestatus']=2;
	}
	if($coupon['limittime']['tend']<TIMESTAMP){
		$coupon['timestatus']=3;
	}
	if($item['couponid']){
		$coupon['couponid']=str_array($coupon['couponid']);
	}
	$coupon['couponid']=$coupon['couponid']?:0;
	$return['coupon']=$coupon;
	$coupons=pdo_getall('jy_weishop_coupon',array('id'=>$coupon['couponid'],'uniacid'=>$_SESSION['uniacid']));
	foreach($coupons as $k=>&$item){
			if($fromuser && $fromuser!=$_SESSION['uid']){
				$t=pdo_count('jy_weishop_coupon_getlog',array('couponid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'gettype'=>1));
				if($item['getmax']>0 && $t<$item['getmax']){
					$this->model('coupon')->getcoupon($item['id'],$fromuser);
				}
			}
			$limittimes2=json_decode($item['limittimes'],true);
			$item=$this->model('coupon')->intcoupon($item['id']);
			if($item['issendtime']){
				$item['sendtime']=$item['sendtime']?json_decode($item['sendtime'],true):array();
				$item['etime']=$item['sendtime']['tend'];
				if($item['sendtime']['tstart']>time()){
					$item['issend']=0;
					$item['etime']=$item['sendtime']['tstart'];
				}elseif($item['sendtime']['tstart']<time() && $item['sendtime']['tend']>time()){
					$item['issend']=1;
					$item['etime']=$item['sendtime']['tend'];
				}elseif($item['sendtime']['tend']<time()){
					$item['issend']=2;
				}
				$item['sendtime']['tend2']=$item['sendtime']['tend'];
				$item['sendtime']['tstart2']=$item['sendtime']['tstart'];
				$item['sendtime']['tend']=date('Y-m-d',$item['sendtime']['tend']);
				$item['sendtime']['tstart']=date('Y-m-d',$item['sendtime']['tstart']);
			}else{
				$item['issend']=1;
			}
			if($item['getmax'] > 0){
				$num = pdo_count('jy_weishop_coupon_getlog',array('uniacid' => $_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'couponid'=>$item['id']));
				if($num<$item['getmax']){
					$item['canget'] = 1;
				}else{
					$item['canget'] = 0;
				}
			}else{
				$item['canget'] = 1;
				$item['getmax'] = '不限';
			}
			if($item['timelimittype']==2 && $limittimes2['tend']<time()){
				unset($getcoupon[$k]);
			}else{
				$coupon[]=$item;
			}	
	}
	$return['total']=count($coupons);
	$return['coupons']=$coupons;
	$return['list']=$list;
	$return['sharedata']=array(
			'title'=>$coupon['sharetitle']?:'分享转优惠券',
			'desc'=>$coupon['sharedesc']?:'分享转优惠券！',
			'link'=>custom_url('jy_weishop/coupon.share//id='.$coupon['id'].'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($coupon['shareicon'])
	);
	set_html('title',$coupon['sharetitle']?:'分享转优惠券');
	exi($return);
}
?>